<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<%@include file="../header.jsp" %>
<script>

	var currentPage = 1;
	$(function(){
		bindProvider();
		bindBill(currentPage);
		bindEvent();
	})
	
	//绑定供应商下拉列表
	function bindProvider(){
		$.post("listProvider",function(msg){
			var str = "";
			$(msg).each(function(i){
				var p = msg[i]
				str += "<option value='"+p.id+"'>"
				str += p.proname
				str += "</option>"
			})
			$("#providerId").append(str)
		},"json")
	}
	
	//绑定订单列表数据
	function bindBill(currentPage){
		var productName = $("#productName").val();
		var providerId = $("#providerId").val();
		var isPayment = $("#isPayment").val();
		
		$.post("listBill",{"currentPage" : currentPage,"productName" : productName,"providerId" : providerId,"isPayment" : isPayment},function(msg){
			var str = "";
			$(msg.list).each(function(i){
				var b = msg.list[i]
				str += "<tr>"
				str += "<td>"	
				str += "<span>"+b.billcode+"</span>"	
				str += "</td>"	
				str += "<td>"	
				str += "<span>"+b.productname+"</span>"
				str += "</td>"	
				str += "<td>"	
				str += "<span>"+b.provider.proname+"</span>"	
				str += "</td>"
				str += "<td>"	
				str += "<span>"+b.totalprice+"</span>"	
				str += "</td>"	
				str += "<td>"	
				str += "<span>"	
				str += b.ispayment == 0 ? "未付款" : "已付款"	
				str += "</span>"	
				str += "</td>"	
				str += "<td>"	
				str += "<span>"	
				str += b.creationdate
				str += "</span>"	
				str += "</td>"	
				str += "<td>"	
				str += '<span><a class="viewBill" href="javascript:;" data-id="'+b.id+'"><img src="statics/images/read.png" alt="查看" title="查看"/></a></span>'
				str += '<span><a class="modifyBill" href="javascript:;" data-id="'+b.id+'"><img src="statics/images/xiugai.png" alt="修改" title="修改"/></a></span>'
				str += '<span><a class="deleteBill" href="javascript:;" data-id="'+b.id+'"><img src="statics/images/schu.png" alt="删除" title="删除"/></a></span>'
				str += "</td>"	
				str += "</tr>"
			})
			$(".providerTable tr:not(:first)").remove();
			$(".providerTable").append(str)
			//显示总记录数
			$("#count").html(msg.count)
			//显示当前页
			$("#currentPage").html(currentPage)
			//显示总页数
			$("#totalPage").html(msg.totalPage)
		},"json")
	}
	
	function bindEvent(){
		//查看订单明细 delegate
		$(".providerTable").delegate(".viewBill","click",function(){
			var id = $(this).data("id")
			window.location = "getBillById?id="+id
		})

		$(".providerTable").delegate(".modifyBill","click",function(){
			var id = $(this).data("id")
			window.location = "toBillModify?id="+id
		})

		
		var id;
		
		//删除明细
		$(".providerTable").delegate(".deleteBill","click",function(){
			$(".zhezhao,.remove").show();
			id = $(this).data("id")
		})
	
		//确定删除
		$("#yes").click(function(){
			window.location = "deleteBillById?id="+id
		})
	
		//取消删除
		$("#no").click(function(){
			$(".zhezhao,.remove").hide();
		})
	
		//查询按钮
		$("#searchbutton").click(function(){
			currentPage = 1;
			bindBill(currentPage)
		})
		
		//首页
		$(".first").click(function(){
			currentPage = 1;
			bindBill(currentPage)
		})
		
		//上一页
		$(".prev").click(function(){
			if(currentPage == 1){
				return
			}
			currentPage--;
			bindBill(currentPage)
		})
		
		//下一页
		$(".next").click(function(){
			if(currentPage == parseInt($("#totalPage").html())){
				return
			}
			currentPage++;
			bindBill(currentPage)
		})
		
		//末页
		$(".last").click(function(){
			currentPage = parseInt($("#totalPage").html());
			bindBill(currentPage)
		})
	}
</script>

<c:if test="${requestScope.updateFlag > 0 }">
	<script>
		alert("修改成功")
	</script>
</c:if>
<c:if test="${requestScope.updateFlag == 0 }">
	<script>
		alert("修改失败")
	</script>
</c:if>

<c:if test="${requestScope.deleteFlag > 0 }">
	<script>
		alert("删除成功")
	</script>
</c:if>
<c:if test="${requestScope.deleteFlag == 0 }">
	<script>
		alert("删除失败")
	</script>
</c:if>
	<div class="right">
       <div class="location">
           <strong>你现在所在的位置是:</strong>
           <span>订单管理页面</span>
       </div>
       <div class="search">
			<span>商品名称：</span>
			<input id="productName" type="text" value="">
			<span>供应商：</span>
			<select id="providerId">
				   <option value="0">--请选择--</option>
       		</select>
			<span>是否付款：</span>
			<select id="isPayment">
				<option value="-1">--请选择--</option>
				<option value="0" >未付款</option>
				<option value="1" >已付款</option>
       		</select>
			 <input	value="查 询" type="submit" id="searchbutton">
			 <a href="toBillAdd">添加订单</a>
       </div>
       <!--账单表格 样式和供应商公用-->
      <table class="providerTable" cellpadding="0" cellspacing="0">
          <tr class="firstTr">
              <th width="10%">订单编码</th>
              <th width="20%">商品名称</th>
              <th width="10%">供应商</th>
              <th width="10%">订单金额</th>
              <th width="10%">是否付款</th>
              <th width="10%">创建时间</th>
              <th width="30%">操作</th>
          </tr>
     
				
			
      </table>
      <div class="page-bar">
			<ul class="page-num-ul clearfix">
				<li>共<span id="count"></span>条记录&nbsp;&nbsp; <span id="currentPage"></span>/<span id="totalPage"></span>页</li>
				
					<a href="javascript:void(0)" class="first">首页</a>
					<a href="javascript:void(0)" class="prev">上一页</a>
			
				
					<a href="javascript:void(0)" class="next">下一页</a>
					<a href="javascript:void(0)" class="last">最后一页</a>
				
				&nbsp;&nbsp;
			</ul>
		 <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key" />页
	     <button type="button" class="page-btn" onClick=''>GO</button>
		</span>
		</div> 
        </div>
    </section>

	<!--点击删除按钮后弹出的页面-->
	<div class="zhezhao"></div>
	<div class="remove" id="removeBi">
		<div class="removerChid">
			<h2>提示</h2>
			<div class="removeMain">
				<p>你确定要删除该订单吗？</p>
				<a href="javascript:void(0)" id="yes">确定</a>
				<a href="javascript:void(0)" id="no">取消</a>
			</div>
		</div>
	</div>
<%@include file="../foot.jsp" %>